<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Db;

class Publishapi extends Controller
{
    public function publish(Request $request){
        $Globalkey = Db::name('system')->field('publishkey')->find();
        if(!$Globalkey){
            return jsonmsg(404,"未配置安全密钥!");
        }
        $data = $request->param();
        if(empty($data)){
            return jsonmsg(404,"MuYuCMS!");
        }
        if(!password_verify($data['publishkey'],$Globalkey['publishkey'])){
            return jsonmsg(-1,"安全密钥错误!");
        }
        unset($data['publishkey']);
        if(empty($data['mid'])){
            return jsonmsg(-1,"栏目未指定!");
        }
        if(!isset($data['matpattern'])){
            $data['matpattern'] = 1;
        }
        if(!isset($data['price'])){
            $data['price'] = 1;
        }
        $lms = Db::name("category")->find($data["mid"]);
        if(empty($lms)){
            return jsonmsg(-1,"栏目不存在!");
        }
        if(empty($data['modid'])){
            $data['modid'] = Db::name('model')->field('id,tablename')->find($lms['modid']);
        }else if(is_numeric($data['modid'])){
            $data['modid'] = Db::name('model')->field('id,tablename')->find($data['modid']);
        }else{
            $m = Db::name('model')->where(['tablename'=>$data['modid']])->field('id,tablename')->find();
            if($m){
                $data['modid'] = $m;
            }else{
                return jsonmsg(-1,"模型不存在!");
            }
        }
        $checkfiel = Db::name('modfiel')->where(['modid'=>$data['modid']['id'],'required'=>1])->field('field,title')->select();
        $countf = "";
        foreach($checkfiel as $vals){
            if(!isset($data[$vals['field']])){
                $countf .= $vals['field']."(".$vals['title'].")".",";
            }
        }
        if(!empty($countf)){
            return jsonmsg(-1,"缺少必填项:".substr($countf,0,strlen($countf)-1));
        }
        $tab = $data['modid']['tablename'];
        unset($data['modid']);
        if(isset($data['editor'])){
            $data['editor'] = html_entity_decode(htmlspecialchars_decode($data['editor']));
        }
        if(!isset($data['create_time'])){
            $data['create_time'] = time();
        }else if(!isset($data['update_time'])){
            $data['update_time'] = time();
        }
        $setid = setconid();
        $data['id'] = $setid['id'] + 1;
        unset($setid);
        $ord = Db::name($tab)->select();//当前模型表文章自动排序
        if($ord != NULL || $ord != ""){
            foreach($ord as $key=>$val){
                $data['orders'] = $val["orders"] + 1;
            }
        }
        unset($ord);
        $data['maturl']	= '/matcont_'.$data['id'].'.html';
        if($lms['patterncontents'] == 2 || $data['matpattern'] == 2 && $data['price'] == 1){
            $filename = $this->filenamesrule($lms['filenamesrule'],$data['id'],$lms['thiswhere']);
            $data['maturl']	= '/'.$lms['topwhere'].'/'.$lms['thiswhere'].'/'.$filename.$lms['filenames'];
        }
        $data['isadmin'] = 1;
        $data['author'] = isset($data['author']) ? $data['author'] : "admin";
        $data['uid'] = isset($data['uid']) ? $data['uid'] : "1";
        $res = Db::name($tab)->insert($data);
        if($res){
            if($lms["pid"] != "0"){
                Db::name("category")->where(["id"=>$lms["pid"]])->setInc("ar_cont");
                Db::name("category")->where(["id"=>$data["mid"]])->setInc("ar_cont");
            }else{
                Db::name("category")->where("id",$data["mid"])->setInc("ar_cont");
            }
            if(Db::name('admin')->find($data['uid'])){
                Db::name("admin_data")->where("uid",$data['uid'])->setInc("contribute");
            }
            //建立新增文章附属表
            Db::name($tab."_data")->insert(['aid'=>$data['id']]);
            //在今日大数据表文章发布数字段自增1
            Db::name('bigdata')->whereTime('create_time','today')->setInc('article_add');
            return jsonmsg(1,"添加成功");
        }else{
            return jsonmsg(0,"添加失败");
        }
    }

    public function getcategory(Request $request)
    {
        $Globalkey = Db::name('system')->field('publishkey')->find();
        if(!$Globalkey){
            return jsonmsg(404,"未配置安全密钥!");
        }
        $data = $request->param();
        if(empty($data)){
            return jsonmsg(404,"MuYuCMS!");
        }else if(!password_verify($data['publishkey'],$Globalkey['publishkey'])){
            return jsonmsg(-1,"安全密钥错误!");
        }
        $category = Db::name('category')->field('id,title')->select();
        $str = '<br>';
        if(!empty($category)){
            foreach($category as $key=>$v){
                $str .= '<p>'.$v['id'].'<a>'.$v['title'].'</a></p>';
            }
        }
        $str .= '</br>';
        return $str;
    }
}